﻿using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XQ.WeChatOfficialAccount.BLL.Common
{
    public class ZipHelper
    {
        /// <summary>
        /// 打包文件
        /// </summary>
        /// <param name="fileNames">待报包文件集合</param>
        /// <param name="compresssionLevel"></param>
        /// <param name="saveFullPath"></param>
        /// <param name="password"></param>
        /// <param name="comment"></param>
        public static void FilesZip(List<string> fileNames, int? compresssionLevel, string saveFullPath, string password = "", string comment = "")
        {
            using (ZipOutputStream zos = new ZipOutputStream(File.Open(saveFullPath, FileMode.OpenOrCreate)))
            {
                if (compresssionLevel.HasValue)
                {
                    zos.SetLevel(compresssionLevel.Value);//设置压缩级别
                }

                if (!string.IsNullOrEmpty(password))
                {
                    zos.Password = password;//设置zip包加密密码
                }

                if (!string.IsNullOrEmpty(comment))
                {
                    zos.SetComment(comment);//设置zip包的注释
                }

                foreach (string file in fileNames)
                {
                    if (System.IO.File.Exists(file))
                    {
                        FileInfo item = new FileInfo(file);
                        FileStream fs = System.IO.File.OpenRead(item.FullName);
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);

                        ZipEntry entry = new ZipEntry(item.Name);
                        zos.PutNextEntry(entry);
                        zos.Write(buffer, 0, buffer.Length);
                        fs.Close();
                    }
                }
                zos.Close();
            }
        }

        /// <summary>
        /// 打包文件
        /// </summary>
        /// <param name="fileNames">包含原文件名和打包后的实际文件名</param>
        /// <param name="compresssionLevel"></param>
        /// <param name="saveFullPath"></param>
        /// <param name="password"></param>
        /// <param name="comment"></param>
        public static void FilesZip(Dictionary<string, string> fileNames, int? compresssionLevel, string saveFullPath, string password = "", string comment = "")
        {
            using (ZipOutputStream zos = new ZipOutputStream(File.Open(saveFullPath, FileMode.OpenOrCreate)))
            {
                if (compresssionLevel.HasValue)
                {
                    zos.SetLevel(compresssionLevel.Value);//设置压缩级别
                }

                if (!string.IsNullOrEmpty(password))
                {
                    zos.Password = password;//设置zip包加密密码
                }

                if (!string.IsNullOrEmpty(comment))
                {
                    zos.SetComment(comment);//设置zip包的注释
                }

                foreach (KeyValuePair<string, string> file in fileNames)
                {
                    if (System.IO.File.Exists(file.Key))
                    {
                        FileInfo item = new FileInfo(file.Key);
                        FileStream fs = System.IO.File.OpenRead(item.FullName);
                        byte[] buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);

                        ZipEntry entry = new ZipEntry(file.Value + Path.GetExtension(item.FullName));
                        zos.PutNextEntry(entry);
                        zos.Write(buffer, 0, buffer.Length);
                        fs.Close();
                    }
                }
                zos.Close();
            }
        }
    }
}
